import React, {Component} from "react";
import {connect} from 'react-redux';

import i18n from '../translations'
import NavigationService from '../components/NavigationService';
import Navigator from './navigation/Navigator';


class AppView extends Component {

  render() {
    return (
      <Navigator
        onNavigationStateChange={(prevState, currentState) => {
          const currentScene = this.getCurrentRouteName(currentState);
          if (currentScene === i18n.t('tabs.balance') || currentScene === i18n.t('tabs.my')) {
            if (!this.props.isLogin) {
              const prevScene = this.getCurrentRouteName(prevState);
              NavigationService.navigate('Login', {prev: prevScene})
            } else {
              console.log('已登录')
            }
          }

        }}
        uriPrefix="/app"
        ref={navigatorRef => {
          NavigationService.setTopLevelNavigator(navigatorRef);
        }}
      />
    );
  }

  getCurrentRouteName(navigationState) {
    if (!navigationState) {
      return null;
    }
    const route = navigationState.routes[navigationState.index];
    if (route.routes) {
      return this.getCurrentRouteName(route);
    }
    return route.routeName;
  }

}

const mapStateToProps = (state) => {
  return {
    isLogin: state.member.isLogin
  }
};

export default connect(mapStateToProps, null)(AppView);